package com.spirng.leetcode.day05;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;

public class Test347 {
    public int[] topKFrequent(int[] nums, int k) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i],map.getOrDefault(nums[i],0)+1);
        }
         PriorityQueue<int []> pq=new PriorityQueue<>((pair1,pair2)->pair1[1]-pair2[1]);
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
              if(pq.size()<k){
                  pq.add(new int[]{entry.getKey(), entry.getValue()});
              }else {
                  if(entry.getValue()>pq.peek()[1]){
                      pq.poll();
                      pq.add(new int[]{entry.getKey(), entry.getValue()});
                  }
              }
        }
        int [] arr =new int[k];
        for (int i = 0; i < k; i++) {
            arr[i]=pq.poll()[0];
        }
        return arr;
    }
    public int[] twoSum(int[] nums, int target) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int cut=target- nums[i];
            if(map.containsKey(cut)){
                return new int[]{map.get(cut),i};
            }
            map.put(nums[i],i);
        }
        return new int[]{};
    }
}
